package war2100.game.states.menu;

import java.util.ArrayList;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

public class MenuButton {
	private int x, y;
	private int width, height;
	private String text;
	private ArrayList <MenuButtonListener> listener;
	private final static int DEFAULT_BUTTON_WIDTH 			= 200;
	private final static int DEFAULT_BUTTON_HEIGHT 			= 50;
	private final static int DEFAULT_SPACE_BETWEEN_BUTTONS	= 20;
	
	public MenuButton(GameContainer gc, int x, int y, int width, int height, String text, MenuButtonListener l) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.text = text;
		this.listener = new ArrayList<MenuButtonListener>();
		this.addListener(l);
	}
	
	public MenuButton(GameContainer gc, String text, int index, MenuButtonListener l) {		
		this(gc, (gc.getWidth() / 2) - (DEFAULT_BUTTON_WIDTH / 2), 50 + (index * (DEFAULT_BUTTON_HEIGHT + DEFAULT_SPACE_BETWEEN_BUTTONS)), DEFAULT_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT, text, l);
	}

	public void update(GameContainer gc, int delta, boolean clicked) throws SlickException {
		int mx = gc.getInput().getMouseX();
		int my = gc.getInput().getMouseY();
		//boolean clicked = gc.getInput().isMousePressed(Input.MOUSE_LEFT_BUTTON);
		
		if (clicked == true && mx >= x && mx <= x+width && my >= y && my <= y+height) {
			Logger.getLogger(MenuButton.class.getName()).log(Level.INFO, "Button {0} Pressed", this.toString());
			if (listener != null) {
				for (MenuButtonListener l : listener) {
					l.MenuButtonClicked();
				}
			}
		}
	}

	public void render(GameContainer gc, Graphics g) throws SlickException {
		g.setColor(Color.gray);
		g.fillRect(x, y, width, height);
		g.setColor(Color.blue);
		g.drawString(text, x+5, y+5);
	}
	
	public void addListener(MenuButtonListener l) {
		this.listener.add(l);
	}
	
	public void removeListener(MenuButtonListener l) {
		this.listener.remove(l);
	}
	
	public String toString() {
		return "MenuButton: "+text;
	}
}
